package cn.vendgateway.com.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ConfigLoggerConfiguration implements ApplicationListener<ApplicationReadyEvent> {

    private final Environment environment;

    public ConfigLoggerConfiguration(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("===== 应用启动成功，开始打印所有配置 =====");
        for (PropertySource<?> propertySource : ((org.springframework.core.env.AbstractEnvironment) environment).getPropertySources()) {
            log.info("配置来源: {}", propertySource.getName());
            if (propertySource.getSource() instanceof java.util.Map) {
                ((java.util.Map<?, ?>) propertySource.getSource()).forEach((k, v) -> {
                    log.info("{} = {}", k, v);
                });
            }
        }
        log.info("===== 配置打印结束 =====");
    }
}
